//
//  UserAccountManager.m
//  ProjectName
//
//  Created by zfc on 2019/12/6.
//  Copyright © 2019年 depelec. All rights reserved.
//



#import "UserAccountManager.h"
@implementation UserAccountManager


static UserAccountManager *userAccountManager = nil;

+ (instancetype)shared {
    if (userAccountManager == nil) {
        userAccountManager = [[UserAccountManager alloc] init];
    }
    return userAccountManager;
}

+ (instancetype)allocWithZone:(struct _NSZone *)zone {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        userAccountManager = [super allocWithZone:zone];
    });
    return userAccountManager;
}

- (id)copyWithZone:(NSZone *)zone
{
    return self;
}
- (id)mutableCopyWithZone:(NSZone *)zone
{
    return self;
}


- (id)init {
    if (self = [super init]) {
        _user = [self parseUser];
        _userinfo = [self parseUserinfo];
    }
    return self;
}

- (LoginModel *)user {
    return [self parseUser];
}

- (UserInfoModel *)userinfo {
    return [self parseUserinfo];
}

- (BOOL)is_login {
    if (self.user.member.ID) {
        return YES;
    }
    return NO;
}

//清除保存的信息
- (void)exitAccount {    
    LoginModel *model = [[LoginModel alloc] init];
    [NSKeyedArchiver archiveRootObject:model toFile:kAccountPath];
    self.user = nil;

    UserInfoModel *model1 = [[UserInfoModel alloc] init];
    [NSKeyedArchiver archiveRootObject:model1 toFile:kUserinfoPath];
    self.userinfo = nil;
}

// 从归档中去取出登录信息
- (LoginModel *)parseUser {
    LoginModel *model = [NSKeyedUnarchiver unarchiveObjectWithFile:kAccountPath];
    return model;
}

// 用户登录信息归档
- (void) saveUser:(LoginModel *)model {
    [NSKeyedArchiver archiveRootObject:model toFile:kAccountPath];
}

// 用户个人信息归档
- (void) saveUserinfo:(UserInfoModel *)model {
    [NSKeyedArchiver archiveRootObject:model toFile:kUserinfoPath];
}

// 从归档中去取出个人信息
- (UserInfoModel *)parseUserinfo {
    UserInfoModel *model = [NSKeyedUnarchiver unarchiveObjectWithFile:kUserinfoPath];
    return model;
}
@end
